package org.jeecg.modules.wlhy.company.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.company.entity.HyCompanyEtcConfig;
import org.jeecg.modules.wlhy.company.service.IHyCompanyEtcConfigService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: hy_comapny_etc_config
 * @Author: jeecg-boot
 * @Date:   2025-03-29
 * @Version: V1.0
 */
@Api(tags="hy_comapny_etc_config")
@RestController
@RequestMapping("/company/hyCompanyEtcConfig")
@Slf4j
public class HyCompanyEtcConfigController extends JeecgController<HyCompanyEtcConfig, IHyCompanyEtcConfigService> {
	 @Autowired
	 private IHyCompanyEtcConfigService hyCompanyEtcConfigService;
	 @Autowired
	 private UserUtil userUtil;

	 /**
	  * 分页列表查询
	  *
	  * @param HyCompanyEtcConfig
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value = "hy_comapny_etc_config-分页列表查询", notes = "hy_comapny_etc_config-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> queryPageList(HyCompanyEtcConfig HyCompanyEtcConfig,
									@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
									@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<HyCompanyEtcConfig> queryWrapper = QueryGenerator.initQueryWrapper(HyCompanyEtcConfig, req.getParameterMap());
		 Page<HyCompanyEtcConfig> page = new Page<HyCompanyEtcConfig>(pageNo, pageSize);
		 IPage<HyCompanyEtcConfig> pageList = hyCompanyEtcConfigService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  * 添加
	  *
	  * @param hyCompanyEtcConfig
	  * @return
	  */
	 @AutoLog(value = "hy_comapny_etc_config-添加")
	 @ApiOperation(value = "hy_comapny_etc_config-添加", notes = "hy_comapny_etc_config-添加")
	 @PostMapping(value = "/add")
	 public Result<?> add(@RequestBody HyCompanyEtcConfig hyCompanyEtcConfig) {
		 if (hyCompanyEtcConfigService.count(new LambdaQueryWrapper<HyCompanyEtcConfig>().eq(HyCompanyEtcConfig::getCompanyId, hyCompanyEtcConfig.getCompanyId()).eq(HyCompanyEtcConfig::getReportType, hyCompanyEtcConfig.getReportType()))>0) {
			 hyCompanyEtcConfig.setUpdateBy(userUtil.getUser().getRealname());
			 hyCompanyEtcConfig.setUpdateTime(new Date());
			 hyCompanyEtcConfigService.update(hyCompanyEtcConfig, new LambdaQueryWrapper<HyCompanyEtcConfig>().eq(HyCompanyEtcConfig::getCompanyId, hyCompanyEtcConfig.getCompanyId()).eq(HyCompanyEtcConfig::getReportType, hyCompanyEtcConfig.getReportType()));
		 }else {
			 hyCompanyEtcConfig.setCreateBy(userUtil.getUser().getRealname());
			 hyCompanyEtcConfig.setCreateTime(new Date());
			 hyCompanyEtcConfigService.save(hyCompanyEtcConfig);
		 }
		 return Result.OK("操作成功！");
	 }

	 /**
	  * 编辑
	  *
	  * @param hyCompanyEtcConfig
	  * @return
	  */
	 @AutoLog(value = "hy_comapny_etc_config-编辑")
	 @ApiOperation(value = "hy_comapny_etc_config-编辑", notes = "hy_comapny_etc_config-编辑")
	 @PutMapping(value = "/edit")
	 public Result<?> edit(@RequestBody HyCompanyEtcConfig hyCompanyEtcConfig) {
		 hyCompanyEtcConfigService.updateById(hyCompanyEtcConfig);
		 return Result.OK("编辑成功!");
	 }

	 /**
	  * 通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "hy_comapny_etc_config-通过id删除")
	 @ApiOperation(value = "hy_comapny_etc_config-通过id删除", notes = "hy_comapny_etc_config-通过id删除")
	 @DeleteMapping(value = "/delete")
	 public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		 hyCompanyEtcConfigService.removeById(id);
		 return Result.OK("删除成功!");
	 }

	 /**
	  * 批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "hy_comapny_etc_config-批量删除")
	 @ApiOperation(value = "hy_comapny_etc_config-批量删除", notes = "hy_comapny_etc_config-批量删除")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		 this.hyCompanyEtcConfigService.removeByIds(Arrays.asList(ids.split(",")));
		 return Result.OK("批量删除成功!");
	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @ApiOperation(value = "hy_comapny_etc_config-通过id查询", notes = "hy_comapny_etc_config-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		 HyCompanyEtcConfig HyCompanyEtcConfig = hyCompanyEtcConfigService.getById(id);
		 if (HyCompanyEtcConfig == null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(HyCompanyEtcConfig);
	 }

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param hyCompanyEtcConfig
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request, HyCompanyEtcConfig hyCompanyEtcConfig) {
		 return super.exportXls(request, hyCompanyEtcConfig, HyCompanyEtcConfig.class, "hy_comapny_etc_config");
	 }

	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 return super.importExcel(request, response, HyCompanyEtcConfig.class);
	 }

	 /**
	  * 通过companyId和reportType查询
	  *
	  * @param companyId
	  * @return
	  */
	 @ApiOperation(value = "hy_comapny_etc_config-通过companyId和reportType查询", notes = "hy_comapny_etc_config-通过companyId和reportType查询")
	 @GetMapping(value = "/getEtcConfigByCompanyIdAndReportType")
	 public Result<?> queryByCompanyIdAndReportType(@RequestParam(name = "companyId", required = true) String companyId,
													@RequestParam(name = "reportType", required = true) String reportType) {
		 if (StringUtils.isEmpty(companyId) || StringUtils.isEmpty(reportType)) {
		 	 return Result.error("参数不能为空");
		 }

		 QueryWrapper<HyCompanyEtcConfig> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("company_id", companyId);
		 queryWrapper.eq("report_type", reportType);
		 HyCompanyEtcConfig hyCompanyEtcConfig = hyCompanyEtcConfigService.getOne(queryWrapper);	// 一个分公司只会有一个配置
		 if (hyCompanyEtcConfig == null) {
			 return Result.OK();
		 }else {
			 return Result.OK(hyCompanyEtcConfig);
		 }

	 }
 }